﻿using System;

namespace XEvolve.Core.Runtime
{
    public class AdapterFactory<SourceType, TargetType> : IAdapterFactory
    {
        private readonly Func<SourceType, TargetType> func;

        public AdapterFactory(Func<SourceType, TargetType> func)
        {
            this.func = func;
        }

        public object CreateAdapter(object adaptee, Type adapterType)
        {
            return func((SourceType) adaptee);
        }

        public bool CanCreateAdapter(object adaptee, Type adapterType)
        {
            return adapterType == typeof (TargetType) && adaptee is SourceType;
        }
    }
}
